/**
 * 
 */
package Media.ui.widgets.DeviceView;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.concurrent.ConcurrentHashMap;


import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Shell;


import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Display;

import agg.UIUtils;

import Library.*;
import Media.ui.UIExtendable;
import Media.ui.UIInterface;




/**
 * @author Hardeep
 *
 */
public class AddDevice extends UIExtendable {

//	private Shell shell = null;
	private Label lblDevName = null;
	private Text txtName = null;
	private Label lblDevIcon = null;
	private List listIcons = null;
	private Canvas canvasIconDisplay = null;
	private Button btnCreate = null;
	private Button btnCancel = null;
	
	private char sep = File.separatorChar;
//	private String icons_path = sep + "ui" + sep + "media" + sep + 
//		"dev_icons" + sep;
	private String iconsPath = UIUtils.icon_path.replace('/', sep);
	private String curDir = ".";
	private String iconExt = ".png";
	
	private ConcurrentHashMap<String, Image> icons = 
		new ConcurrentHashMap<String, Image>();
	
	private DeviceView dv;
	
	public AddDevice(DeviceView dv) {
		try {
			curDir = new File (".").getCanonicalPath();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		this.dv = dv;
		
		createShell();
	}

	/**
	 * This method initializes shell
	 */
	public void createShell() {
		GridData dataBtnCancel = new GridData();
		dataBtnCancel = UIUtils.createGridData(false, false, GridData.FILL, 
				GridData.CENTER, -1, -1, -1, -1, -1, -1, -1, -1);
		
		GridData dataBtnCreate = new GridData();
		dataBtnCreate = UIUtils.createGridData(true, false, GridData.FILL, 
				GridData.CENTER, -1, -1, -1, -1, -1, -1, -1, -1);
		
		GridData dataLblDevName = new GridData();
		dataLblDevName = UIUtils.createGridData(true, false, GridData.END, 
				GridData.CENTER, -1, -1, -1, -1, -1, -1, -1, -1);
		
		GridData dataLblDevIcon = new GridData();
		dataLblDevIcon = UIUtils.createGridData(false, false, GridData.END, 
				GridData.BEGINNING, -1, -1, -1, -1, -1, -1, -1, -1);
		
		GridData dataTxtName = new GridData();
		dataTxtName = UIUtils.createGridData(true, false, GridData.FILL, 
				GridData.CENTER, 2, -1, -1, -1, -1, -1, -1, -1);
		
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 3;
		
		shell = new Shell(SWT.CLOSE | SWT.TITLE);
		shell.setText("Add New Device");
		shell.setLayout(gridLayout);
		shell.setSize(new Point(320, 180));
		shell.addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent arg0) {closeShell();}
		});
		
		lblDevName = new Label(getShell(), SWT.NONE);
		lblDevName.setText("Device Name:");
		lblDevName.setLayoutData(dataLblDevName);
		
		txtName = new Text(getShell(), SWT.BORDER);
		txtName.setLayoutData(dataTxtName);
		
		lblDevIcon = new Label(getShell(), SWT.NONE);
		lblDevIcon.setText("Device Icon:");
		lblDevIcon.setLayoutData(dataLblDevIcon);
		
		createListIcons();
		
		createCanvasIconDisplay();
		
		UIUtils.addFillerLabel(shell);
		
		btnCreate = new Button(getShell(), SWT.NONE);
		btnCreate.setText("Create");
		btnCreate.setLayoutData(dataBtnCreate);
		btnCreate.addSelectionListener(new SelectionListener() {
			public void widgetDefaultSelected(SelectionEvent arg0) {
				widgetSelected(arg0);
			}
			public void widgetSelected(SelectionEvent arg0) {
				String[] sels = listIcons.getSelection();
				if ( sels.length > 0 ) {
					String sel = sels[0];
					if ( icons.containsKey(sel) ) {
						Device n = dv.addDevice(txtName.getText());
						try {
							n.setProperty("icon", sel + iconExt);
						} catch (SQLException e) {
							// TODO notify user in GUI
							e.printStackTrace();
						}
						System.out.println("Created new Device:");
						System.out.println("\tName: " + txtName.getText());
						System.out.println("\tIcon: " + n.getProperty("icon"));
						dv.updateImages();
					}
					else {
						UIUtils.showWarning(shell, 
								"Invalid Icon. Please choose another.");
					}
				}
				closeShell();
			}
		});
		shell.setDefaultButton(btnCreate);
		
		btnCancel = new Button(getShell(), SWT.NONE);
		btnCancel.setText("Cancel");
		btnCancel.setLayoutData(dataBtnCancel);
		btnCancel.addSelectionListener(new SelectionListener() {
			public void widgetDefaultSelected(SelectionEvent arg0) {
				widgetSelected(arg0);
			}
			public void widgetSelected(SelectionEvent arg0) {
				closeShell();
			}
		});
	}

	/**
	 * Initialize listIcons
	 */
	private void createListIcons() {
		GridData dataListIcons = new GridData();
		dataListIcons = UIUtils.createGridData(true, true, GridData.FILL, 
				GridData.FILL, -1, -1, -1, -1, -1, -1, -1, -1);
		
		listIcons = new List(getShell(), SWT.V_SCROLL | SWT.BORDER);
		listIcons.setLayoutData(dataListIcons);
		
		// get all the icons
		File f = new File(curDir + sep + "src" + iconsPath);
		String[] files = f.list();
		ImageData iconData;
		
		for ( String cur_icon : files ) {
			if ( cur_icon.toLowerCase().endsWith(iconExt) ) {
				// TODO check if the 'src' in path is actually necessary on release
				iconData = new ImageData(curDir + sep + "src" + iconsPath + cur_icon);
				cur_icon = cur_icon.substring(0, cur_icon.length() - 4);
				
				listIcons.add(cur_icon);
				icons.put(cur_icon, new Image(Display.getCurrent(),iconData));
			}
		}
		
		// add selection listener
		listIcons.addSelectionListener(new SelectionListener() {
			public void widgetDefaultSelected(SelectionEvent arg0) {
				widgetSelected(arg0);
			}

			public void widgetSelected(SelectionEvent arg0) {
				canvasIconDisplay.redraw();
			}			
		});
		
		listIcons.select(0);
	}

	/**
	 * This method initializes canvasIconDisplay	
	 */
	private void createCanvasIconDisplay() {
		GridData dataCanvas = new GridData();
		dataCanvas = UIUtils.createGridData(true, false, GridData.FILL, 
				GridData.FILL, -1, -1, -1, -1, -1, -1, -1, -1);
		
		
		canvasIconDisplay = new Canvas(getShell(), SWT.BORDER);
		canvasIconDisplay.setBackground(new Color(Display.getCurrent(), 
				255, 255, 255));
		canvasIconDisplay.setLayoutData(dataCanvas);
		
		canvasIconDisplay.addPaintListener(new PaintListener() {
			public void paintControl(PaintEvent e) {
				String[] sels = listIcons.getSelection();
				if ( sels.length > 0 ) {
					String sel = sels[0];
					if ( icons.containsKey(sel) ) {
						Image iconImage = icons.get(sel);
						int y = (e.height / 2) - (dv.icon_size[0] / 2);
						int x = (e.width / 2) - (dv.icon_size[1] / 2);
						e.gc.drawImage(iconImage,x,y);	
					}
				}
			}
		});
	}

}
